<?php
    namespace app\controllers;
    use app\common\BaseController;
    use app\models\Charge;
    use app\models\ChargeTypes;
    use app\models\Menu;
    use app\models\Product;
    use app\models\Store;

    class ChargeController extends BaseController{
        public function actionTypes(){
            $chargeTypeList = ChargeTypes::find()->asArray()->all();
            $parentIds = [];
            foreach ($chargeTypeList as $key => $value){
                if ($value['pid'] == 0){
                    array_push($parentIds, $value);
                }
            }
            foreach ($parentIds as $key => $value){
                $parentIds[$key]['children'] = [];
                foreach ($chargeTypeList as $k => $v){
                    if ($v['pid'] == $parentIds[$key]['id']){
                        array_push($parentIds[$key]['children'], $v);
                    }
                }
            }
            return $this->result($parentIds);
        }

        public function actionAdd(){
            $postData = \Yii::$app->request->post();
            $chargeType = new ChargeTypes();
            $postData['ctime'] = time();
            $code = 1;
            if ($chargeType->load($postData, '') && $chargeType->save()){
                $code = 0;
            }
            return $this->result([], $code);
        }

        public function actionAddCharge(){
            $postData = \Yii::$app->request->post('req', '');
            $postData = json_decode($postData, true);
            $chargeObj = new Charge();
            $chargeObj->storeId = $postData['store']['id'];
            $chargeObj->picture = $postData['store']['picture'];
            $chargeObj->ctime = time();
            $flag = 0;
            if ($chargeObj->save()){
                foreach ($postData['menuList'] as $key => $value){
                    $menuObj = new Menu();
                    $menuObj->productId = $value['id'];
                    $menuObj->count = $value['count'];
                    $menuObj->chargeId = $chargeObj->id;
                    $menuObj->ctime = time();
                    if (!$menuObj->save()){
                        $flag = 1;
                    }
                }
            }else{
                $flag = 1;
            }
            return $this->result([], $flag);
        }

        public function actionList(){
            $page = \Yii::$app->request->get('page', 1);
            $pageSize = \Yii::$app->request->get('pageSize', 10);
            $offset = ($page - 1) * $pageSize;
            $allCharges = Charge::find()->offset($offset)->limit($pageSize)->asArray()->all();
            $allCount = Charge::find()->count();
            foreach ($allCharges as $key => $value){
                //获取当前比消费的商店名称
                $allCharges[$key]['storeName'] = Store::find()->select('name')->where([
                    'id' => $value['storeId']
                ])->asArray()->one()['name'];
                $allCharges[$key]['allPrice'] = $this->calcAllPrice($value['id']);
            }
            return $this->result([
                'list' => $allCharges,
                'total' => $allCount
            ]);
        }

        /*
         * 数据返回少，但是查询多，还有一个累加效果，所以就用了一个函数
         * CREATE FUNCTION GET_CHARGE_PRICE
                (CHARGE_ID INT)
                RETURNS INT
            BEGIN
                DECLARE res INT DEFAULT 0;
                SELECT (SELECT price from product WHERE id=productId)*count + res INTO res FROM menu WHERE chargeId=CHARGE_ID;
                RETURN res;
            END
            set global log_bin_trust_function_creators=TRUE;
         */
        private function calcAllPrice($id){
            $res = \Yii::$app->db->createCommand("SELECT (SELECT price from product WHERE id=productId)*count as price FROM menu WHERE chargeId=$id;")->queryAll();
            $allPrice = 0;
            if ($res && count($res) > 0){
                foreach ($res as $key => $value){
                    $allPrice += $value['price'];
                }
            }
            return $allPrice;
        }
    }